﻿Shader "Unlit/Diffuse_Vertix"
{
    Properties
    {
       /* _MainTex ("Texture", 2D) = "white" {}*/
        _Diffuse("Diffuse",Color)=(1,1,1,1)
    }
    SubShader
    {
       /* Tags { "RenderType"="Opaque" }
        LOD 100*/

        Pass
        {
            Tags{"LightMode"="ForwardBase"}
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            //#pragma multi_compile_fog

            #include"UnityCG.cginc"
            #include"Lighting.cginc"

            fixed4 _Diffuse;

            struct appdata
            {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
                //float4 vertex : POSITION;
                //float2 uv : TEXCOORD0;
            };
            struct v2f
             {
                 float4 pos : SV_POSITION;
                 float3 color : COLOR;
             };

//以下是顶点着色器，我们需要关注如何实现一个逐顶点的漫反射光照，因此漫反射部分在顶点着色器中进行
            v2f vert(appdata v) {
                v2f o;
                // Transform the vertex from object space to projection space
                o.pos = UnityObjectToClipPos(v.vertex);
                // Get ambient term
                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
                // Transform the normal from object space to world space
                fixed3 worldNormal = normalize(mul(v.normal, (float3x3)unity_WorldToObject));
                // Get the light direction in world space
                fixed3 worldLight = normalize(_WorldSpaceLightPos0.xyz);
                // Compute diffuse term
                fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormal, worldLight));
                o.color = ambient + diffuse;
                return o;
            }
    //所有的计算都在顶点着色器中完成了，因此片元着色器代码比较简单，我把颜色输出即可        
            fixed4 frag(v2f i) : SV_Target 
            {
                return fixed4(i.color, 1.0);
            }
            ENDCG
        }
    }
}
